<?php

App::uses('AppController', 'Controller');

/**
 * CoreGroupsBlocks Controller
 *
 * @property CoreGroupsBlock $CoreGroupsBlock
 */
class CoreGroupsBlocksController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->CoreGroupsBlock->recursive = 0;
        $this->set('coreGroupsBlocks', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->CoreGroupsBlock->exists($id)) {
            throw new NotFoundException(__('Invalid core groups block'));
        }
        $options = array('conditions' => array('CoreGroupsBlock.' . $this->CoreGroupsBlock->primaryKey => $id));
        $this->set('coreGroupsBlock', $this->CoreGroupsBlock->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->CoreGroupsBlock->create();
            if ($this->CoreGroupsBlock->save($this->request->data)) {
                $this->Session->setFlash(__('The core groups block has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The core groups block could not be saved. Please, try again.'));
            }
        }
        $coreGroups = $this->CoreGroupsBlock->CoreGroup->find('list');
        $coreBlocks = $this->CoreGroupsBlock->CoreBlock->find('list');
        $this->set(compact('coreGroups', 'coreBlocks'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->CoreGroupsBlock->exists($id)) {
            throw new NotFoundException(__('Invalid core groups block'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->CoreGroupsBlock->save($this->request->data)) {
                $this->Session->setFlash(__('The core groups block has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The core groups block could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('CoreGroupsBlock.' . $this->CoreGroupsBlock->primaryKey => $id));
            $this->request->data = $this->CoreGroupsBlock->find('first', $options);
        }
        $coreGroups = $this->CoreGroupsBlock->CoreGroup->find('list');
        $coreBlocks = $this->CoreGroupsBlock->CoreBlock->find('list');
        $this->set(compact('coreGroups', 'coreBlocks'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->CoreGroupsBlock->id = $id;
        if (!$this->CoreGroupsBlock->exists()) {
            throw new NotFoundException(__('Invalid core groups block'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->CoreGroupsBlock->delete()) {
            $this->Session->setFlash(__('Core groups block deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Core groups block was not deleted'));
        $this->redirect(array('action' => 'index'));
    }
    
    public function getGroupsIdFromBlock($blockId){
        $options = array('conditions' => array('CoreGroupsBlock.core_blocks_id'=> $blockId));
        $data = $this->CoreGroupsBlock->find('all', $options);
        $groups = array();
        foreach ($data as $value) {
            $groups[] = intval($value["CoreGroupsBlock"]["core_groups_id"]);
        }
        return $groups;
    }

}
